package vm


// 浮点字节(Floating Point Byte)的编码与解码


/*
** converts an integer to a "floating point byte", represented as
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
** eeeee != 0 and (xxx) otherwise.
 */
func Int2fb(x int) int {

 	e := 0	/* exponent */
 	if x < 8 {
 		return x
 	}

 	for x >= (8 << 4) {  /* coarse steps */
 		x = (x + 0xf) >> 4  /* x = ceil(x / 16) */
 		e += 4
 	}

 	for x >= (8 << 1) {  /* fine steps */
 		x = (x + 1) >> 1  /* x = ceil(x / 2) */
 		e++
 	}

 	return ((e + 1)  << 3) | (x - 8)
 }


 /* converts back */
 func Fb2Int(x int) int {

 	if x < 8 {
 		return x
 	} else {
 		return ((x & 7) + 8) << uint((x>>3)-1)
 	}
 }